package cn.joybreeze.controller;


import cn.joybreeze.common.result.R;
import cn.joybreeze.common.vo.AnswerQueryVO;
import cn.joybreeze.model.AnswerDO;
import cn.joybreeze.model.OptionsDO;
import cn.joybreeze.model.QuestionDO;
import cn.joybreeze.service.AnswerService;
import cn.joybreeze.service.OptionsService;
import cn.joybreeze.service.QuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kjwaa
 * @since 2021-09-15
 */
@RestController
@RequestMapping("/answer-do")
public class AnswerController {

    @Autowired
    AnswerService answerService;

    @Autowired
    QuestionService questionService;

    @Autowired
    OptionsService optionsService;

    @PostMapping("/add")
    public R addAnswer(@RequestBody AnswerQueryVO answerQueryVO){
        // 先看又没有对应数据
        QueryWrapper<AnswerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", answerQueryVO.getQuestionId());
        queryWrapper.eq("result", answerQueryVO.getResult());
        AnswerDO answerDO = answerService.getOne(queryWrapper);
        if(answerDO != null){
            answerDO.setCnumber(answerDO.getCnumber() + 1);
        }else{
            answerDO = new AnswerDO();
            BeanUtils.copyProperties(answerQueryVO, answerDO);
        }
        boolean isSuccess = answerService.saveOrUpdate(answerDO);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 答案统计
     * @param questionnaireId
     * @return
     */
    @GetMapping("/statistic/{questionnaireId}")
    public R statistic(@PathVariable int questionnaireId){
        // 返回数据
        List<Map<String, Object>> data = new ArrayList<>();
        // 根据问卷id 和 type <= 2 查询问题列表
        QueryWrapper<QuestionDO> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq("questionnaire_id", questionnaireId);
        questionQueryWrapper.le("type", 2);
        List<QuestionDO> questionDOList = questionService.list(questionQueryWrapper);
        for (QuestionDO questionDO : questionDOList) {
            Map<String, Object> map = new HashMap<>();
            // 根据问题id查询选项列表
            QueryWrapper<OptionsDO> optionsQueryWrapper = new QueryWrapper<>();
            optionsQueryWrapper.eq("question_id", questionDO.getId());
            List<OptionsDO> optionsDOList = optionsService.list(optionsQueryWrapper);
            String[] xData = new String[optionsDOList.size()];
            int[] yData = new int[optionsDOList.size()];
            for(int i = 0; i < optionsDOList.size(); i++){
                OptionsDO optionsDO = optionsDOList.get(i);
                xData[i] = optionsDO.getContent();
                // 根据问题id 和 选项内容 来 查询答案
                QueryWrapper<AnswerDO> answerQueryWrapper = new QueryWrapper<>();
                answerQueryWrapper.eq("question_id", optionsDO.getQuestionId());
                answerQueryWrapper.eq("result", optionsDO.getContent());
                AnswerDO answerDO = answerService.getOne(answerQueryWrapper);
                if(answerDO == null){
                    yData[i] = 0;
                }else{
                    yData[i] = answerDO.getCnumber();
                }
            }
            // 封装到questionMap中
            map.put("questionTitle", questionDO.getQuestionTitle());
            map.put("type", questionDO.getType());
            map.put("xData", xData);
            map.put("yData", yData);
            // 封装到data中
            data.add( map);
        }
        return R.ok(data);
    }


}
